package mobweb.booktrack;

import java.io.Serializable;

/**
 * This activity should be passed a Book instance via serializable intent
 */

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class BookActivity extends Activity {
    
    private final Activity ac = this; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_book_layout);
	// Show the Up button in the action bar.
	// getActionBar().setDisplayHomeAsUpEnabled(true);

	Intent i = getIntent();
	Bundle extras = i.getExtras();
	Serializable sBook = extras.getSerializable("book");
	Book b;
	try {
	    b = (Book) sBook;
	} catch (ClassCastException e) {
	    b = null;
	    e.printStackTrace();
	}
	if (b != null) {
	    drawBook(b);
	} else {
	    Toast.makeText(getBaseContext(), "Opps something went wrong",
		    Toast.LENGTH_LONG).show();
	    // TODO goback to parent here
	}
    }

    private void drawBook(final Book b) {
	TextView title = (TextView) findViewById(R.id.textTitle);
	TextView author = (TextView) findViewById(R.id.textAuthor);
	TextView description = (TextView) findViewById(R.id.textDesc);
	TextView state = (TextView) findViewById(R.id.textStatus);
	TextView category = (TextView) findViewById(R.id.textCategory);
	// TODO Imageview preview image of the book
	title.setText(b.getBookName());
	author.setText(b.getAuthor());
	description.setText(b.getDescription());
	String state_;
	switch (b.getFlag()) {
	case Book.READ:
	    state_ = "You have read this book";
	    break;
	case Book.READING:
	    state_ = "You are reading this book";
	    break;
	case Book.WISH_TO_READ:
	    state_ = "You wish to read this book";
	    break;
	default:
	    state_ = "Unknown";
	}
	state.setText(state_);
	category.setText(b.getCategory());

	Button read = (Button) findViewById(R.id.button_read);
	Button wish_read = (Button) findViewById(R.id.button_wishread);
	Button reading = (Button) findViewById(R.id.button_reading);
	Button delete = (Button) findViewById(R.id.button_delete);

	read.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		b.setFlag(Book.READ);
		b.writeToDB(getBaseContext());
		drawBook(b);
	    }

	});

	wish_read.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		b.setFlag(Book.WISH_TO_READ);
		b.writeToDB(getBaseContext());
		drawBook(b);
	    }

	});

	reading.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		b.setFlag(Book.READING);
		b.writeToDB(getBaseContext());
		drawBook(b);
	    }

	});
	
	delete.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		b.deleteFromDB(getBaseContext());
		Toast.makeText(getBaseContext(), "Book deleted succesfully", Toast.LENGTH_SHORT).show();
		NavUtils.navigateUpFromSameTask(ac);
	    }

	});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.activity_book_layout, menu);
	return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case android.R.id.home:
	    // This ID represents the Home or Up button. In the case of this
	    // activity, the Up button is shown. Use NavUtils to allow users
	    // to navigate up one level in the application structure. For
	    // more details, see the Navigation pattern on Android Design:
	    //
	    // http://developer.android.com/design/patterns/navigation.html#up-vs-back
	    //
	    NavUtils.navigateUpFromSameTask(this);
	    return true;
	}
	return super.onOptionsItemSelected(item);
    }

}
